﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace mitasoft.ERS.Business
{
    public partial class ERSEntities : global::System.Data.Objects.ObjectContext
    {

        #region IssueType

        /// <summary>
        /// Get all issue types
        /// </summary>
        /// <returns></returns>
        public List<Business.IssueType> GetIssueTypeList()
        {
            var queryIssueType = from iType in this.IssueTypes
                                 select iType;

            return queryIssueType.ToList();
        }

        /// <summary>
        /// Get Issue type for a issues
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Business.IssueType GetIssueTypeByID(int id)
        {
            return this.IssueTypes.Where(item => item.ID == id).Select(item => item).FirstOrDefault();
        }

        #endregion

        #region IssueStatus

        /// <summary>
        /// Get all issue statuses
        /// </summary>
        /// <returns></returns>
        public List<Business.IssueStatu> GetIssueStatuses()
        {
            var queryIssueStatuses = from iStatus in this.IssueStatus
                                     select iStatus;

            return queryIssueStatuses.ToList();
        }

        /// <summary>
        /// Get issue status for a issue
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Business.IssueStatu GetIssueStatusByID(int id)
        {
            return this.IssueStatus.Where(item => item.ID == id).Select(item => item).FirstOrDefault();
        }
       
        #endregion

        #region Projects

        /// <summary>
        /// Get all projects
        /// </summary>
        /// <returns></returns>
        public List<Business.Project> GetProjects()
        {
            var queryProjects = from proj in this.Projects
                                select proj;

            return queryProjects.ToList();
        }

        /// <summary>
        /// Get project by ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Business.Project GetProjectByID(int id)
        {
            return this.Projects.Where(item => item.ID == id).Select(item => item).FirstOrDefault();
        }
       
        #endregion

        #region Issue

        /// <summary>
        /// Get Issue by id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Business.Issue GetIssueByID(int id)
        {
            return this.Issues.Where(item => item.ID == id).FirstOrDefault();
        }

        /// <summary>
        /// Insert issue from submit issue form
        /// </summary>
        /// <param name="title"></param>
        /// <param name="description"></param>
        /// <param name="issueType"></param>
        /// <param name="idProject"></param>
        public void InsertIssueFromSubmit(string title, string description, int issueType, int idProject)
        {
            Issue issue = new Issue();

            issue.Description = description;
            issue.Title = title;
            issue.IssueType = GetIssueTypeByID(issueType);
            issue.IssueStatu = GetIssueStatusByID(1); //open
            issue.Project = GetProjectByID(idProject);

            this.Issues.AddObject(issue);
        }

        #endregion

        #region VW_IssueDetail

        /// <summary>
        /// Get Issue Detail
        /// </summary>
        /// <param name="idIssue"></param>
        /// <returns></returns>
        public Business.VW_IssueDetail GetIssueDetail(int idIssue)
        {
            return this.VW_IssueDetail.Where(item => item.IDIssue == idIssue).FirstOrDefault();
        }

        #endregion
    }
}
